Thread: [Help] A program to count the number of words in a sentence!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    [Help] A program to count the number of words in a sentence!

    I am trying to make a program which will return the number of words in a given sentence. It should be able to give correct information even if there are multiple spaces before the sentence, between words or after the sentence. I haven't been able to make it handle multiple spaces after the sentence, because it will always give me the number of words +1.
    I thought this could be because it was passing the \0 in the end of the string, so I tried to fill all the empty slots in the string with \0.
    I would appreciate if you could help me understand what I am doing wrong!
    The program goes as follow:

    Code:
    #include <stdio.h>
    
    
    void stuff(char s[], int n){
        int i = 0;
        while (s[i] != '\0'){
            i++;
            }
        for ( i; i < n; i++){
            s[i] = '\0';
        }
    }
    // A function to count words:
    int wcount( char s[] ){
        int i = 0;
        int num = 1;
        while ( s[i] == ' ' ){
            i++; // Preventing errors in case of multiple spaces before the sentence.
        }
        while ( s[i] != '\0' ){
            if ( s[i] == ' '){
                num++;
                while ( s[i] == ' ' ){
                        i++; // Preventing errors if there are multiple spaces between words.
                }
            }
            i++;
        }
        return( num );
    }
    
    void main(){
        int const DIM = 100;
        char str[DIM];
        gets(str);
        stuff(str, DIM);
        printf( "The sentence has %d words \n", wcount(str) );
    }
    Thank you very much in advance!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're counting the words AFTER you've already scanned off the word with a char. That's a trickier logic, imo.

    Increment the word counter right when you hit the first letter of the word - that's when you're inside the word. Then don't do anything with the word counter, until you have come to a space or newline, (a multi-line sentence will have a word at the end of the row, with a newline, but no space after it, before the next word).

    Aha!

    When you reach the space of newline, you come outside the word, (you can use a flag for inside/outside), now again, the first letter you come to (or digit if you prefer), is a new word, so increment your word counter.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Remove the multiple spaces...
    Code:
    char * ptr;
    do
      { 
         ptr = strstr(string,"  ");
         if (ptr)
           *ptr = '.';
       }
    while (ptr);
    Now you can count the spaces and get the right answer.
    Code:
    words = 0;
    char* ptr = string;
    
    while (*ptr != 0;)
      {
       if (*ptr == ' ')
         words++;
       ++ptr;
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what would I use to count number of words
    By s_jsstevens in forum C Programming
    Replies: 42
    Last Post: 08-11-2009, 07:14 AM
  2. what would I use to count number of words
    By yuuh in forum C Programming
    Replies: 1
    Last Post: 08-11-2009, 07:10 AM
  3. Count number of uppercase words in array
    By stevedawg85 in forum C++ Programming
    Replies: 12
    Last Post: 04-10-2006, 12:38 AM
  4. Replies: 5
    Last Post: 09-28-2004, 12:38 PM
  5. How to count the words in sentence ?
    By Th3-SeA in forum C Programming
    Replies: 1
    Last Post: 10-01-2003, 01:34 AM